<?php
/**
 * Created by PhpStorm.
 * User: Digvijay
 * Date: 10/18/14
 * Time: 5:37 PM
 */

class Contact extends MY_Home {
    public function index() {
        $cartDetails = $this->getCartDetails();
        $clientName = $this->isClientLoggedIn();
        $categories = $this->findAll("Category");
        $branchs = $this->findAll("Branch");
        $terms = $this->findContentByType(Contents::CONTENT_TERMS_AND_CONDITION);
        foreach ($categories as $category) {
            if ($category->getParent() == null) {
                $noOfSub = 0;
                foreach ($categories as $sub) {
                    if ($sub->getParent() != null && $sub->getParent()->getId() == $category->getId()) {
                        $noOfSub++;
                    }
                }
                $category->setNoOfSub($noOfSub);
            }
        }
        $data = array("categories" => $categories, "user" => $clientName, "cart" => $cartDetails, "branchs" => $branchs, "terms" => $terms);
        $this->load->view('contact', $data);
    }

    public function contactMail() {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            $notification = $this->findById("Notification", 1);

            $name = $this->input->post("name");
            $email = $this->input->post("email");
            $website = $this->input->post("website");
            $subject = $this->input->post("subject");
            $comments = $this->input->post("comments");

            $msg = "You got Message From AnmolCards.net Contact Form <br/>";
            $msg .= "Name : ". $name ."<br/>";
            $msg .= "Email : ". $email . "<br/>";
            $msg .= "Website :". $website . "<br/>";
            $msg .= "Subject :". $subject . "<br/>";
            $msg .= "Message :". $comments ."<br/>";

            $arr = array("sent" => false);
            if ($this->sendMail($notification->getOrderPlaceEmail(), "Message From AnmolCards.net Contact Form.", $msg)) {
                $arr["sent"] = true;
            }
            $response->setData($arr);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }
} 